package com.tbfs.common;





import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.acegisecurity.GrantedAuthority;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tbfs.admin.vo.Userinfo;
import com.tbfs.security.CurrentUser;
import com.tbfs.security.IUserDetails;
import com.tbfs.util.PaginationBean;
import com.tbfs.util.PaginationContext;
import com.tbfs.util.ValidateUtil;

public abstract class BaseAction extends ActionSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 
	 * @param className
	 * @return
	 */
	public Object getBean(String className) {
		
		//ApplicationContext ac = new ClassPathXmlApplicationContext();
		ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(getRequest().getSession().getServletContext());
		return ac.getBean(className);
		 
	}
	
	/**
	 * 
	 * @param className
	 * @return
	 */
	public Object getBean(Class clazz) {
		//ApplicationContext ac = new ClassPathXmlApplicationContext();
		ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(getRequest().getSession().getServletContext());
		return ac.getBean(clazz.getName());
	}

	//得到用户信息
	public Userinfo  getUserInfo(){
		Map session = ActionContext.getContext().getSession();
		Userinfo userInfo = (Userinfo)session.get("userinfo");
		return userInfo;
	}

	//设置用户信息
	public void setUserInfo(Userinfo userInfo){
		Map session = ActionContext.getContext().getSession();
		session.put("userinfo", userInfo);
	}
	
	/**
	 * 判断角色的数据访问权限,如果有多种角色时，默认选择最大级别
	 * ROLE_ADMIN ，ROLE_U_COM_ADMIN，ROLE_U_COM_MANAGER 可访问所有数据-0
	 * ROLE_U_SUBCOM_ADMIN，ROLE_U_SUBCOM_MANAGER 只能访问本公司数据-1
	 * ROLE_USER 只能访问自己录入的数据-2
	 * @return -1-无权限,0-访问所有数据，1-本公司数据，2-本人数据
	 * role ROLE_ADMIN-系统管理员-1，
	 * ROLE_U_COM_ADMIN-总公司管理员-2，
	 * ROLE_U_SUBCOM_ADMIN-分公司管理员-3，
	 * ROLE_U_COM_MANAGER-总公司经理-4，
	 * ROLE_U_SUBCOM_MANAGER-分公司经理-5
	 * * ROLE_USER-普通用户-6，
	 */
	public String volidateAccessDataAuth(){
		String flag = "-1";
		IUserDetails userdetails = CurrentUser.getUser();
		GrantedAuthority[] authorities = userdetails.getAuthorities();
		if(authorities!=null && authorities.length>0){
			for(int i=0;i<authorities.length;i++){
				GrantedAuthority auth = authorities[i];
				String authority = auth.getAuthority();
				if("ROLE_ADMIN".equals(authority)){
					flag = "0";
					break;
				}
				if("ROLE_U_COM_ADMIN".equals(authority)){
					flag = "0";
					break;
				}
				
				if("ROLE_U_COM_MANAGER".equals(authority)){
					flag = "0";
					break;
				}
				
				if("ROLE_U_SUBCOM_ADMIN".equals(authority)){
					flag = "1";
					break;
				}
				if("ROLE_U_SUBCOM_MANAGER".equals(authority)){
					flag = "1";
					break;
				}
				if("ROLE_USER".equals(authority)){
					flag = "2";
					break;
				}
			}
		}
		
		return flag;
	}
	

	protected void processPaginationBean() {
		String t_row = getRequest().getParameter("rows");
		if(t_row==null || "".equals(t_row)){
			t_row = "10";
		}
		String t_page = getRequest().getParameter("page");
		if(t_page==null || "".equals(t_page)){
			t_page = "1";
		}
		int rows = Integer.parseInt(t_row);
		int page = Integer.parseInt(t_page);	
		PaginationBean paginationBean = new PaginationBean();
		paginationBean.setPageNo(page);
		paginationBean.setPageSize(rows);
		paginationBean.setStartIndex(paginationBean.getStart());
		PaginationContext.setPaginationBean(paginationBean);
	}
	
	/**
	 * 获得分页PaginationBean的方法
	 * @return
	 */
	protected PaginationBean getPaginationBean(){
		return PaginationContext.getPaginationBean();
	}

	/**
	 * 清除分页PaginationBean的方法
	 */
	protected void clearPaginationBean(){
		PaginationContext.clearPaginationBean();
	}
	
	/**
	 * 取得HttpRequest的简化方法.
	 */
	public static HttpServletRequest getRequest()
	{
		return ServletActionContext.getRequest();
	}
	
	
	/**
	 * 取得HttpRequest的简化方法.
	 */
	public static HttpServletResponse getResponse()
	{
		return ServletActionContext.getResponse();
	}
	/**
	 * 根据jsp页面的属性名称获取数组封装后的List集合
	 * @param request
	 * @param name
	 * @return
	 */
	protected List getParameterValuesList(HttpServletRequest request, String name){
		List list = null;
		String[] array = request.getParameterValues(name);
		if (array != null && array.length > 0) {
			list = new ArrayList();
			for (int i = 0; i < array.length; i++) {
				list.add(array[i]);
			}
		}
		return list;
	}
	/**
	 * 往request里设置属性
	 *
	 * @param request
	 * @param attributeName
	 *            属性名
	 * @param attributeValue
	 *            属性值
	 */
	protected void setAttribute(HttpServletRequest request,
			String attributeName, Object attributeValue) {
		request.setAttribute(attributeName, attributeValue);
	}
	

	/**
	 * 设置异常信息
	 *
	 * @param request
	 * @param e
	 */
	protected void setError(HttpServletRequest request, Exception e) {
		request.getSession().setAttribute("ERROR", e);
	}


	/**
	 * 设置成功信息
	 *
	 * @param request
	 */
	protected void setSuccess(HttpServletRequest request) {
		setAttribute(request, "SUCCESS", "SUCCESS");
	}

	
	/**
	 * 判断是否不需要关闭跳转后的jsp窗口
	 *
	 * @param request
	 * @return
	 */
	protected boolean isNotClose(HttpServletRequest request) {
		String notclose = request.getParameter("notclose");
		if (ValidateUtil.isNotNull(notclose)) {
			return true;
		}
		return false;
	}
	/**
	 * 设置跳转到jsp后不关闭窗口
	 *
	 * @param request
	 */
	protected void setNotClose(HttpServletRequest request) {
		if(isNotClose(request)){
			setAttribute(request,"notclose",	"notclose");
		}
	}
	/**
	 * 向bean进行value的封装
	 * @param bean
	 * @param request
	 */
	protected void populate(Object bean, HttpServletRequest request){
		try {
			BeanUtils.populate(bean, request.getParameterMap());
		} catch (Exception e) {
			//logger.error(getStackTraceMessage(e));
			throw new RuntimeException(e.getMessage(), e);
		}
	}
}